package com.sgam.mf.dao.entity;

import java.util.List;

import org.hibernate.criterion.Order;

import org.apache.log4j.Logger;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import com.sgam.mf.dao.GenericDAOImpl;
import com.sgam.mf.domain.entity.VreportContractPayments;
import com.sgam.mf.helper.ProxyCurrentUser;



@Repository
public class VreportContractPaymentsDAOImpl extends GenericDAOImpl<VreportContractPayments> implements VreportContractPaymentsDAO{
	
	protected final Logger log = Logger.getLogger(getClass());
	

	
	@SuppressWarnings("unchecked")
	public List<VreportContractPayments> list(ProxyCurrentUser user, Integer pUid) {
		log.debug("***********************************************");
		log.debug("ContractPaymentsDAOImpl - list");
		log.debug(user.toString());
		//log.debug(listType);
		log.debug("***********************************************");
		String whereForSecurity = "";
		
		
		
//		if (user.getUserRoleId() == 0 || user.getUserRoleId() == 1){ //If user is Administrator or Manager
			whereForSecurity = " where 1=1 ";
//		}
		
//		if (user.getUserRoleId() == 2){ //If user	Consultant OC
//			whereForSecurity = " where ref_status_fk <> 0 ";
//		}	
		
//		if (user.getUserRoleId() == 2){ //If user	Consultant OC
//			whereForSecurity = " where respons_consultant_fk = " + user.getUserId() + " ";
//		}	
		
//		if (user.getUserRoleId() == 3){ //If user	Personal 
//			whereForSecurity = " where ref_status_fk not in (0 , 1)  and respons_personal_fk = " + user.getUserId() + " ";
//		}		
		
//		if (user.getUserRoleId() == 4){ //If user	Consultant OT 
//			whereForSecurity = " where respons_consultant_fk = " + user.getUserId() + " ";
//		}	
//		
//		if(listType==1) {
//			whereForSecurity = whereForSecurity + " and contract_no != '' "; 
//		}
		
//		List<VreportContractPayments>  result = getCurrentSession().createQuery("from VreportContractPayments " ).list();
		
		List<VreportContractPayments> result = getCurrentSession().createCriteria(VreportContractPayments.class)
				.add(Restrictions.or(
				        Restrictions.eq( "contractatFlg", new Integer(1) ),
				        Restrictions.eq( "finalizatFlg", new Integer(1))
				        )
				     )
				.add( Restrictions.eq("uid", pUid ) )  
				.add( Restrictions.gt("uprc", 0 ) )
	            .setProjection( Projections.projectionList()
	            		.add( Projections.groupProperty("id"), 		"id"	)
	            		.add( Projections.groupProperty("contractNo"), "contractNo"  )
	            		.add( Projections.groupProperty("contractDate"), "contractDate"  )
	            		.add( Projections.groupProperty("amount"), "amount"  )
	            		.add( Projections.groupProperty("sum"), "sum"  )
	            		.add( Projections.groupProperty("uprc"), "uprc"  )
	            		.add( Projections.groupProperty("contractPerson"), "contractPerson"  )
	            		.add( Projections.sum("paydAmount")	, 			"paydAmount"		)
	            		.add( Projections.sum("ord"), "ord"  )
               	)
               	.addOrder( Order.asc("ord") )
               	.setResultTransformer(Transformers.aliasToBean(VreportContractPayments.class)).list();
		return result;
	}


}

	
